Imports ATSMS
Imports ATSMS.SMS
Imports System.Data.SqlClient


Public Class MainForm

    Private Const PHONE_BOOK As String = "phonebook.txt"

    Private m_strPhoneBook As String

    Private WithEvents oGsmModem As New GSMModem

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        If cboComPort.Text = String.Empty Then
            MsgBox("COM Port must be selected", MsgBoxStyle.Information)
            Return
        End If

        oGsmModem.Port = cboComPort.Text

        If cboBaudRate.Text <> String.Empty Then
            oGsmModem.BaudRate = Convert.ToInt32(cboBaudRate.Text)
        End If

        If cboDataBit.Text <> String.Empty Then
            oGsmModem.DataBits = Convert.ToInt32(cboDataBit.Text)
        End If

        If cboStopBit.Text <> String.Empty Then
            Select Case cboStopBit.Text
                Case "1"
                    oGsmModem.StopBits = Common.EnumStopBits.One
                Case "1.5"
                    oGsmModem.StopBits = Common.EnumStopBits.OnePointFive
                Case "2"
                    oGsmModem.StopBits = Common.EnumStopBits.Two
            End Select
        End If

        If cboFlowControl.Text <> String.Empty Then
            Select Case cboFlowControl.Text
                Case "None"
                    oGsmModem.FlowControl = Common.EnumFlowControl.None
                Case "Hardware"
                    oGsmModem.FlowControl = Common.EnumFlowControl.RTS_CTS
                Case "Xon/Xoff"
                    oGsmModem.FlowControl = Common.EnumFlowControl.Xon_Xoff
            End Select
        End If

        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "Connecting to the messaging device..." & ControlChars.CrLf
        
        Try
            oGsmModem.Connect()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
            Return
        End Try

        Try
            oGsmModem.NewMessageIndication = True
        Catch ex As Exception

        End Try

        btnSend.Enabled = True
        btnDisconnect.Enabled = True
        btnConnect.Enabled = False

        oGsmModem.AutoDeleteSentMessage = True
        MsgBox("Connected to the messaging device successfully!", MsgBoxStyle.Information)

        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "Connected to the messaging device successfully!" & ControlChars.CrLf

    End Sub

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CheckForIllegalCrossThreadCalls = False

        Initialize()
    End Sub

    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click

        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "Disconnecting from the messaging device..." & ControlChars.CrLf

        Try
            oGsmModem.Disconnect()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try

        btnSend.Enabled = False
        btnDisconnect.Enabled = False
        btnConnect.Enabled = True

        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "Disconnected from the messaging device successfully!" & ControlChars.CrLf

    End Sub

   
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim msisdn As String = InputBox("Enter a valid phone number", "Bulk SMS Gateway")
        If msisdn <> String.Empty Then
            lstPhoneList.Items.Add(msisdn)
            SavePhoneBook()
        End If

    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        lstPhoneList.Items.Remove(lstPhoneList.SelectedItem)
        SavePhoneBook()
    End Sub

    Private Sub Initialize()
        ' Read from the phone book
        Dim strContent As String
        m_strPhoneBook = Application.StartupPath & "\" & PHONE_BOOK
        If My.Computer.FileSystem.FileExists(m_strPhoneBook) Then
            strContent = My.Computer.FileSystem.ReadAllText(m_strPhoneBook)
            Dim lines() As String = strContent.Split(ControlChars.CrLf)
            Dim i As Integer
            For i = 0 To lines.Length - 1
                lstPhoneList.Items.Add(lines(i))
            Next
        End If
    End Sub

    Private Sub SavePhoneBook()
        Dim strFileContent As String = String.Empty
        For Each item As String In lstPhoneList.Items
            strFileContent += item & ControlChars.CrLf
        Next
        My.Computer.FileSystem.WriteAllText(m_strPhoneBook, strFileContent, False)
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "SMS sending process initialized!" & ControlChars.CrLf

        Dim itemsed As String
        Dim messaged As String
        Dim countered As Integer
        Dim smstotal As Integer

        Dim itemcount As Integer = 0 - (lstPhoneList.Items.Count - 1)

        smstotal = 0

        For j As Integer = itemcount To 0

            countered = 0 - j

            itemsed = lstPhoneList.Items(countered)

            If itemsed.Split("|").Length = 3 Then

                If itemsed.Trim <> String.Empty And itemsed.Split("|")(0).Trim.Length <> 0 And itemsed.Split("|")(1).Trim.Length <> 0 And itemsed.Split("|")(2).Trim.Length <> 0 Then
                    messaged = "The GPA of " & itemsed.Split("|")(2).Trim & " is " & itemsed.Split("|")(1).Trim

                    If StringUtils.IsUnicode(messaged) Then
                        oGsmModem.Encoding = Common.EnumEncoding.Unicode_16Bit
                    Else
                        oGsmModem.Encoding = Common.EnumEncoding.GSM_Default_7Bit
                    End If
                    txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "Sending SMS...        Number = " & itemsed.Split("|")(0) & ",    GPA = " & itemsed.Split("|")(1) & ",    Student Name = " & itemsed.Split("|")(2) & ControlChars.CrLf
                    oGsmModem.SendSMSToOutbox(itemsed.Split("|")(0).Trim, messaged)
                    smstotal = smstotal + 1

                End If


            End If


            lstPhoneList.Items.Remove(lstPhoneList.Items(countered))
            SavePhoneBook()


        Next

        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & smstotal & " SMSs sent!" & ControlChars.CrLf
        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "SMS sending process completed!" & ControlChars.CrLf


    End Sub

    Private Sub oGsmModem_OutboxSMSSent(ByVal e As ATSMS.OutboxSMSSentEventArgs) Handles oGsmModem.OutboxSMSSent
        If e.ErrorCode > 0 Then
            txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "Error sending SMS to " & e.DestinationNumber & ". " & e.ErrorDescription & ControlChars.CrLf
        Else
            txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "SMS sent to " & e.DestinationNumber & ControlChars.CrLf
        End If
    End Sub

    Private Sub cboComPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboComPort.SelectedIndexChanged

    End Sub

    Private Sub lstPhoneList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPhoneList.SelectedIndexChanged

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cnnVideos As SqlConnection

        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "Connecting to database..." & ControlChars.CrLf




        cnnVideos = New SqlConnection("Server=(local);Initial Catalog=sms;Integrated Security=SSPI")

        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "Connection to database established successfully!" & ControlChars.CrLf



        Dim strVideos As String = "SELECT * FROM student_table, gpa_table WHERE gpa_table.Student_ID = student_table.Student_ID;"
        Dim cmdVideos As SqlCommand = New SqlCommand(strVideos, cnnVideos)

        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "Retrieving data..." & ControlChars.CrLf



        Dim dadVideoCollection As SqlDataAdapter = New SqlDataAdapter
        dadVideoCollection.SelectCommand = cmdVideos

        Dim setVideos As DataSet = New DataSet("VideoCollection")

        dadVideoCollection.Fill(setVideos)

        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & setVideos.Tables(0).Rows.Count & " entries loaded successfully from the database!" & ControlChars.CrLf

        Dim parent_number As String
        Dim gpa As String
        Dim student_name As String
        Dim combinedd As String

        cnnVideos.Open()
        cnnVideos.Close()

        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "Preparing SMSs to be sent..." & ControlChars.CrLf
        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "Validating entries..." & ControlChars.CrLf

        Dim s As Integer = setVideos.Tables(0).Rows.Count - 1
        For i As Integer = 0 To s
            parent_number = setVideos.Tables(0).Rows(i).Item("Parent_Number")
            gpa = setVideos.Tables(0).Rows(i).Item("GPA")
            student_name = setVideos.Tables(0).Rows(i).Item("First_Name") & " " & setVideos.Tables(0).Rows(i).Item("Middle_Name") & " " & setVideos.Tables(0).Rows(i).Item("Last_Name")

            combinedd = parent_number & "|" & gpa & "|" & student_name
            
            If parent_number.Trim.Length > 0 And gpa.Trim.Length > 0 And student_name.Trim.Length > 0 Then
                lstPhoneList.Items.Add(combinedd)
                SavePhoneBook()
            End If
        Next

        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "SMSs ready to be sent!" & ControlChars.CrLf

        txtMsgDeliveryStatus.Text += TimeOfDay & ":  " & "You may now start sending SMSs!" & ControlChars.CrLf

    End Sub
End Class
